004974A0 Network?.ReceiveGameplayPacket




// Note: This function checks for network packets, and queues game
// engine packets in a receive queue
// Note: Protocol control packets are processed immediately
// Note: If a packet is >= sizeOfPayload + 14 (0xE), then the entire
// packet is copied into the Node buffer (up to 512 bytes) 
// Note: only sizeOfPayload + 14 (0xE) bytes will be copied out of the
// node into a buffer when the node is extraced from the receive queue.

Function Network?.ReceiveGameplayPacket
	Do
		recvfrom(bufferSize = 512)
		If (numBytesReceived == SOCKET_ERROR) return 0

		If (numBytesReceived < 18 (0x12)) discardPacket
		If (node.packet.sizeOfPayload + 14 (0xE) > numBytesReceived) discardPacket
		If (CalcChecksum(&packet) != node.packet.checksum) discardPacket

		Switch(packet.packetType)
			Case 1:	// Protocol control packet
				ProcessProtocolControlPacket(from, node)

			Case else: // Game engine command packet
				If ((node.packet.unknown != Network.???) && (node.packet.unknown != 0)) discardPacket
				AddNodeToReceivedQueue		// Add to linked list, guarded by CriticalSection objects
		End Switch
	Loop
End Function



Function Network.ProcessProtocolControlPacket(From *from, Node *node)
	If (node.packet.packetType == 1) // Note, this is a redundant check
		Switch(node.packet.protocolControlPacket.commandType)
			Case 1:
			Case 4: // ReceiveReplicatedPlayersList
			Case 5:
			Case 6:
		End Switch
	Else
		// This case should never execute
	End If
End Function



// Note: Node objects have a 512 byte buffer. The sizeOfPayload field
// is only 1 BYTE. Copy size is sizeOfPayload + 14 (0xE).
// The size of packet headers is 14 (0xE).
Function Network.ExtractReceivedPacketFromQueue(char *destBuffer)
	If (LinkedListReceivedQueue.head == NULL) return 0
	
	ExtractReceivedBufferFromQueue
	CopyReceivedBufferToDestBuffer(destBuffer, sizeOfPayload + 14 (0xE))
End Function



Data Structures
---------------


Node
----
0x0	Node *next
0x4	Node *prev

	NetworkPacket
	-------------
0x08	0x0	DWORD playerNetID
0x0C	0x4	DWORD unknown
0x10	0x8	BYTE sizeOfPayload
0x11	0x9	BYTE packetType
0x12	0xA	DWORD checksum




	ProtocolControlPacket
	---------------------
0x16		DWORD commandType (1-6)



	GameEnginePacket
	----------------



